package com.gdpu.liou.symphonyserver.service.impl;

import com.gdpu.liou.symphonyserver.mapper.ConsumerMapper;
import com.gdpu.liou.symphonyserver.entity.Consumer;
import com.gdpu.liou.symphonyserver.service.ConsumerService;
import com.gdpu.liou.symphonyserver.common.utils.PagingUtil;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ConsumerServiceImpl implements ConsumerService {
    private final ConsumerMapper consumerMapper;

    public ConsumerServiceImpl(ConsumerMapper consumerMapper) {
        this.consumerMapper = consumerMapper;
    }

    /**
     * 添加用户
     */
    @Override
    public boolean insertConsumer(Consumer consumer) {
        return consumerMapper.insertConsumer(consumer) > 0;
    }

    /**
     * 查询用户：根据用户名
     */
    @Override
    public Consumer selectConsumerByName(String username) {
        return consumerMapper.selectConsumerByName(username);
    }

    /**
     * 查询用户分页数据
     */
    @Override
    public PagingUtil pagingQueryConsumer(int currentPage, String username) {
        int totalCount = consumerMapper.selectTotalConsumer(username);
        int pageSize = 6;
        int pageCount = 0;
        pageCount = (totalCount % pageSize == 0 ? (totalCount / pageSize) : (totalCount / pageSize) + 1);
        // 数据起始位置
        int start = (currentPage - 1) * pageSize;
        List<Consumer> data = consumerMapper.selectPagingData(start, pageSize, username);
        PagingUtil pagingUtil = new PagingUtil(pageSize, totalCount, pageCount, currentPage, data);
        return pagingUtil;
    }


    /**
     * 查看密码是否正确
     *
     * @param username
     * @param password
     */
    @Override
    public boolean verifyPassword(String username, String password) {
        return consumerMapper.verifyPassword(username, password) > 0;
    }

    /**
     * 根据账号查询
     *
     * @param username
     */
    @Override
    public Consumer getByUsername(String username) {
        return consumerMapper.getByUsername(username);
    }

    /**
     * 根据主键查询整个对象
     *
     * @param id
     */
    @Override
    public Consumer selectByPrimaryKey(Integer id) {
        return consumerMapper.selectByPrimaryKey(id);
    }


    /**
     * 修改
     *
     * @param consumer
     */
    @Override
    public boolean update(Consumer consumer) {
        return consumerMapper.update(consumer)>0;
    }

    @Override
    public boolean deleteConsumer(int i) {
        return consumerMapper.deleteConsumer(i);
    }

}
